﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.SessionState;
using XYTXL.API;
using System.Text;
using XYTXL.Data;
using Newtonsoft.Json;
using YanWF;
using Newtonsoft.Json.Converters;
using XYTXL.AppCode;

namespace XYTXL.View.Comon
{
    /// <summary>
    /// ComAjaxHandler 的摘要说明
    /// </summary>
    public class ComAjaxHandler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Buffer = true;
            context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";
            context.Response.ContentType = "text/plain";

            string strAction = context.Request.QueryString["Action"] as string;
            if (string.IsNullOrEmpty(strAction))
            {
                strAction = context.Request.Form["Action"] as string;
            }
            //获取首页中的菜单index.aspx

            #region 系统方法

            //获取用户的Json数据
            if (strAction == "GetUserJson")
            {
                string strReturnMSG = HttpContext.Current.Session["UserJson"] as string;
                if (strReturnMSG == null)
                {
                    strReturnMSG = new JH_Auth_UserB().GetUserInfoForJS();
                    CommonHelp.SessionHelper.SetSession("UserJson", strReturnMSG);
                }
                context.Response.Write(strReturnMSG);
            }
            //获取部门的Json数据
            if (strAction == "GetBranchJson")
            {
                string strReturnMSG = HttpContext.Current.Session["BranchJson"] as string;
                if (strReturnMSG == null)
                {
                    strReturnMSG = new JH_Auth_UserB().GetBranchForJS();
                    HttpContext.Current.Session["BranchJson"] = strReturnMSG;
                }
                context.Response.Write(strReturnMSG);
            }
            //获取数据
            if (strAction == "GetJsonData")
            {
                string strTable = context.Request.QueryString["Table"] as string;
                string strQueryCondition = context.Request.QueryString["QueryCondition"] as string;
                DataTable dt = new DataTable();
                dt = new JH_Auth_FormBaseB().GetDTByCommand("SELECT * FROM " + strTable.FilterSpecial() + " WHERE 1=1 " + strQueryCondition.FilterSpecial());

                IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
                timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                string strJson = JsonConvert.SerializeObject(dt, Formatting.Indented, timeConverter).Replace("null", "\"\""); 
                context.Response.Write(strJson);
            }
            //根据表名或条件删除数据
            if (strAction == "UpdateJHZT")
            {
                try
                {
                    string id = context.Request.QueryString["FormID"] as string;
                    //string strTable = context.Request.QueryString["FormID"] as string;
                    new JH_Auth_FormBaseB().ExsSql(" UPDATE xy_jh SET shzt='1'  WHERE  ID=" + id);
                    context.Response.Write("success");
                }
                catch (Exception)
                {

                    context.Response.Write("error");
                }
            }
            if (strAction == "DelData")
            {
                try
                {
                    string strTable = context.Request.QueryString["Table"] as string;
                    string strQueryCondition = context.Request.QueryString["QueryCondition"] as string;
                    new JH_Auth_FormBaseB().ExsSql(" DELETE " + strTable.FilterSpecial() + " WHERE 1=1 AND  " + strQueryCondition.FilterSpecial());
                    context.Response.Write("success");
                }
                catch (Exception)
                {

                    context.Response.Write("error");
                }
            }

            
            //根据FormCode和ID删除数据
            if (strAction == "DelFormData")
            {
                try
                {
                    string strFormCode = context.Request.QueryString["FormCode"] as string;
                    string strFormID = context.Request.QueryString["FormID"] as string;
                    JH_Auth_FormBase FormBaseData = new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == strFormCode);
                    FormHelp FormHelp = new FormHelp();
                    foreach (string ID in strFormID.Split(','))
                    {
                        FormHelp.DelForm(int.Parse(ID), FormBaseData.FormTable);
                    }
                    context.Response.Write("success");
                }
                catch (Exception)
                {

                    context.Response.Write("error");
                }

            }
            //根据附件信息
            if (strAction == "GetFiles")
            {
                string strFileIDS = context.Request.QueryString["FileIDS"] as string;
                List<JH_Auth_FormAttchg> ListFiles = new List<JH_Auth_FormAttchg>();
                ListFiles = new JH_Auth_FormAttchgB().GetEntities("id IN ('" + strFileIDS.ToFormatLike() + "')").ToList();
                string strJson = JsonConvert.SerializeObject(ListFiles);
                context.Response.Write(strJson);

            }

       
       

            #endregion

         

        }

      

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}